using System.Collections.Generic;

namespace WenYu.Core.Errors
{
    /// <summary>
    /// 错误构建器 - 使用流式API构建错误信息
    /// 
    /// 示例：
    /// <code>
    /// var error = ErrorBuilder.Create("S001", "缺少语句结束符")
    ///     .AtPosition(2, 10)
    ///     .WithSource(sourceCode)
    ///     .WithExplanation("在文语中，每条语句必须以句号（。）结束。")
    ///     .AddSuggestion("在第2行末尾加上中文句号（。）")
    ///     .AddKnowledge("查看【01-基础语法】了解语句规则")
    ///     .Build();
    /// </code>
    /// </summary>
    public class ErrorBuilder
    {
        private readonly CompileError _error = new();
        
        /// <summary>
        /// 创建错误构建器
        /// </summary>
        public static ErrorBuilder Create(string errorCode, string title)
        {
            var builder = new ErrorBuilder();
            builder._error.ErrorCode = errorCode;
            builder._error.Title = title;
            return builder;
        }
        
        /// <summary>
        /// 设置错误严重程度
        /// </summary>
        public ErrorBuilder WithSeverity(ErrorSeverity severity)
        {
            _error.Severity = severity;
            return this;
        }
        
        /// <summary>
        /// 设置错误位置
        /// </summary>
        public ErrorBuilder AtPosition(int line, int column)
        {
            _error.Line = line;
            _error.Column = column;
            return this;
        }
        
        /// <summary>
        /// 设置文件路径
        /// </summary>
        public ErrorBuilder InFile(string filePath)
        {
            _error.FilePath = filePath;
            return this;
        }
        
        /// <summary>
        /// 设置源代码
        /// </summary>
        public ErrorBuilder WithSource(string sourceCode)
        {
            _error.SourceCode = sourceCode;
            return this;
        }
        
        /// <summary>
        /// 设置上下文行数
        /// </summary>
        public ErrorBuilder WithContext(int linesBefore = 1, int linesAfter = 1)
        {
            _error.ContextLinesBefore = linesBefore;
            _error.ContextLinesAfter = linesAfter;
            return this;
        }
        
        /// <summary>
        /// 设置错误标记长度
        /// </summary>
        public ErrorBuilder WithErrorLength(int length)
        {
            _error.ErrorLength = length;
            return this;
        }
        
        /// <summary>
        /// 设置问题解释
        /// </summary>
        public ErrorBuilder WithExplanation(string explanation)
        {
            _error.Explanation = explanation;
            return this;
        }
        
        /// <summary>
        /// 添加解决建议
        /// </summary>
        public ErrorBuilder AddSuggestion(string suggestion)
        {
            _error.Suggestions.Add(suggestion);
            return this;
        }
        
        /// <summary>
        /// 添加多个解决建议
        /// </summary>
        public ErrorBuilder AddSuggestions(params string[] suggestions)
        {
            _error.Suggestions.AddRange(suggestions);
            return this;
        }
        
        /// <summary>
        /// 添加相关知识
        /// </summary>
        public ErrorBuilder AddKnowledge(string knowledge)
        {
            _error.RelatedKnowledge.Add(knowledge);
            _error.RelatedKnowledgeUrls.Add(""); // 无URL
            return this;
        }
        
        /// <summary>
        /// 添加带URL的相关知识
        /// </summary>
        public ErrorBuilder AddKnowledgeWithUrl(string knowledge, string url)
        {
            _error.RelatedKnowledge.Add(knowledge);
            _error.RelatedKnowledgeUrls.Add(url);
            return this;
        }
        
        /// <summary>
        /// 添加相关知识（自动生成文档URL）
        /// </summary>
        public ErrorBuilder AddKnowledgeWithDocUrl(string knowledge)
        {
            _error.RelatedKnowledge.Add(knowledge);
            // 自动生成URL
            var url = ErrorDocumentationConfig.GetUserManualUrl(knowledge);
            _error.RelatedKnowledgeUrls.Add(url);
            return this;
        }
        
        /// <summary>
        /// 添加多个相关知识
        /// </summary>
        public ErrorBuilder AddKnowledges(params string[] knowledges)
        {
            foreach (var k in knowledges)
            {
                AddKnowledgeWithDocUrl(k);
            }
            return this;
        }
        
        /// <summary>
        /// 添加更多帮助
        /// </summary>
        public ErrorBuilder AddHelp(string help)
        {
            _error.MoreHelp.Add(help);
            _error.MoreHelpUrls.Add(""); // 无URL
            return this;
        }
        
        /// <summary>
        /// 添加带URL的更多帮助
        /// </summary>
        public ErrorBuilder AddHelpWithUrl(string help, string url)
        {
            _error.MoreHelp.Add(help);
            _error.MoreHelpUrls.Add(url);
            return this;
        }
        
        /// <summary>
        /// 添加示例帮助（自动生成示例URL）
        /// </summary>
        public ErrorBuilder AddExampleHelp(string exampleFile)
        {
            var help = $"示例代码：{exampleFile}";
            var url = ErrorDocumentationConfig.GetExampleUrl(exampleFile);
            _error.MoreHelp.Add(help);
            _error.MoreHelpUrls.Add(url);
            return this;
        }
        
        /// <summary>
        /// 添加多个帮助
        /// </summary>
        public ErrorBuilder AddHelps(params string[] helps)
        {
            foreach (var h in helps)
            {
                AddHelp(h);
            }
            return this;
        }
        
        /// <summary>
        /// 添加上下文信息
        /// </summary>
        public ErrorBuilder AddContext(string key, object value)
        {
            _error.Context[key] = value;
            return this;
        }
        
        /// <summary>
        /// 构建错误对象
        /// </summary>
        public CompileError Build()
        {
            return _error;
        }
    }
}

